package com.wd.tw.service;

import com.wd.tw.config.ModbusUtil;
import com.wd.tw.constants.StatusEnum;
import com.wd.tw.domain.entity.ElevatorCameraSignal;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
public class ModbusService {


    @Resource
    ModbusUtil modbusUtil;

    @Resource
    ElevatorConfigService elevatorConfigService;

    // 模拟的 DI 和 DO 状态
    private boolean[] diStatus = new boolean[8];
    private boolean[] doStatus = new boolean[8];

    @SneakyThrows
    public boolean[] readState(int offset, int num) {
        boolean[] booleans = modbusUtil.readCoilStatus("192.168.100.204", offset, num);
        return booleans;
    }

    /**
     * 呼唤电梯
     *
     * @param elcNum
     */
    // 定时轮询采集
    @SneakyThrows
    public void callEle(int elcNum, boolean status) {
        ElevatorCameraSignal signal = elevatorConfigService.getSignalInfoBySignalId(elcNum);
        if (signal != null) {
            if (elevatorConfigService.getSignalState()) {
                log.info("SendSignal:發送信號[" + signal.getSignalName() + "][" + StatusEnum.getStatus(status) + "]");
                elcNum--;
                boolean result = modbusUtil.writeCoil("192.168.100.204", elcNum, status);
                log.info("发送信号返回:", result);
            } else {
                log.info("DoNotSendSignal:發送信號[" + signal.getSignalName() + "][" + StatusEnum.getStatus(status) + "]");
            }
        } else {
            log.info("Signal is null:" + elcNum);
        }

    }
}
